<?php
/**
 * 重复执行类
 * 定时、重复执行指定的方法
 * @example
 * $re= new Repeat($d,'sendMail',$mail,1,1);//开始时间，方法名，方法参数，时间间隔，重复次数
 * $re->start();
 */


class repeat{


    //function的参数
    private $arr;
    //开始时间
    private $startTime=0;
    //程序主循环的时间间隔，默认2s
    private $checkInterval=2;
    //log路径
    private $path="d:/cLOG.txt";
    //重复间隔
    private $sendInterval;
    //重复次数
    private $times;

    public function setstartTime($sTime){
        $this->startTime=strtotime($sTime);
    }

    private function excute(){
        include_once "mySendMail.php";

        $mail=new MySendMail($this->arr);
        $mail->sendMail();
    }


    /**
     * repeat constructor.
     * @param $startTime 开始时间
     * @param $function 调用方法名
     * @param $fun_arr 调用方法的参数
     * @param int $sendInterval 重复的时间间格
     * @param int $times 重复次数，默认值PHP_INT_MAX
     */
    public function __construct($startTime, $fun_arr,$sendInterval=60,$times=PHP_INT_MAX)
    {

        $this->arr=$fun_arr;
        $this->startTime=strtotime($startTime);
        $this->sendInterval=$sendInterval;
        $this->times=$times;
    }
    private function setConfig($continue,$stagus){
        $reset=<<<reset
<?php
return array(
    "continue"=>$continue,
    "status"=>"$stagus"
);
reset;
        file_put_contents("config.php",$reset);
    }
    public function start(){
        //忽略客户端，执行时间无限制
        ignore_user_abort(1);
        set_time_limit(0);
try{

    //中止参数config=0时停止脚本。
    //运行脚本时，将config初始化为1
    $this->setConfig(1,"start");

    $count=0;//循环计数器
    $countTimes=0;//邮件计数
    while (true ){

        $count++;
        file_put_contents($this->path,$count."\n",FILE_APPEND);

        $now=date("y-m-d H:i:s");
        $now=strtotime($now);
        $config= include "config.php";//读取中止参数
        if(!$config['continue']){
            $msg= "Stopped by someone";
            file_put_contents($this->path,$msg."\n",FILE_APPEND);
            die("<br>Stopped");
            $this->setConfig(0,"Stopped");
        }
        if($now>$this->startTime){
            //时间到
            //次数超过限制就退出。
            if ($countTimes+1>$this->times){

                die("<br><h1 style='color: red'>{$countTimes} Action finished</h1>");

            }


            $this->excute();//执行指定动作

            $this->startTime+=$this->sendInterval;//执行结束，设定下次执行时间
            $countTimes++;//执行计数

            file_put_contents($this->path,date("H:m:s")."==>$countTimes"."\n",FILE_APPEND);
        }else{
            //时间未到
            file_put_contents($this->path,date("H:m:s",$now)."=>".date("H:m:s",$this->startTime)."\n",FILE_APPEND);
            sleep($this->checkInterval);
        }

    }
}catch (Exception $e){
    file_put_contents($this->path,$e->getMessage().$e->getTraceAsString()."/n",FILE_APPEND);
//    file_put_contents("C:\\Users\\ZD-SERVER\\Desktop\\ex.txt",$e->getMessage().$e->getTraceAsString());

}
    }
}